#include "Attack.h"
#include "Patrol.h"

Attack* Attack::pInst=NULL;


Attack::Attack()
{

}

Attack::~Attack()
{

}

Attack* Attack::GetInstance()
{
	if(pInst == NULL)
	{
		pInst = new Attack();
	}

	return pInst;

}

void Attack::Enter(AbstractEnemy* pEnemy)
{
	//set prevoise positon to current position
	PrevPos = pEnemy->GetPosition();

	//set travel distance to 0
	TravDistance = 0.0f;

	//set arrive behaviour on
	pEnemy->GetBehaviours()->SetArrivalOn(true);

	//set target as player
	pEnemy->GetBehaviours()->SetTargetPosition(pEnemy->GetPlayerPos());

}

void Attack::Execute(AbstractEnemy* pEnemy)
{
	//get the new velocity from behaviours
	pEnemy->GetBehaviours()->SetTargetPosition(pEnemy->GetPlayerPos());
	XMFLOAT3 velocity1 = pEnemy->GetVelocity();
	XMFLOAT3 velocity2 = pEnemy->GetBehaviours()->AccumulateBehaviours();

	//add velocities
	velocity1.x += velocity2.x;
	velocity1.z += velocity2.z;

	//set new velocity
	pEnemy->SetVelocity(velocity1);

	//get travel distance
	TravDistance += pEnemy->GetBehaviours()->GetDistance(pEnemy->GetPosition(),PrevPos);
	
	//check travel distance. if diatnce has been travled chnage state
	if(TravDistance >= pEnemy->GetChaseDistance())
	{
		pEnemy->ChangeState(Patrol::GetInstance());
	}

	//set previouse position
	PrevPos = pEnemy->GetPosition();
}

void Attack::Exit(AbstractEnemy* pEnemy)
{
	//reset values
	pEnemy->GetBehaviours()->SetArrivalOn(false);
	PrevPos = XMFLOAT3(0.0f,0.0f,0.0f);
	TravDistance = 0.0f;
}

void Attack::Release()		// Static
{

	if(pInst)
	{
		delete pInst;
		pInst = NULL;
	}
}
